﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Tom.Algorithm
{
    // Interface for stopping conditions
    public interface IGeneticStopCond
    {
        bool IsSatisfied<T>(List<GeneticIndividual<T>> pop);
    }

    // Stopping conditions for max. iterations
    public class MaxIterGeneticStop : IGeneticStopCond
    {
        public readonly int MaxIter;
        public int _NumIter;

        public MaxIterGeneticStop(int maxIter)
        {
            this.MaxIter = maxIter;
            this._NumIter = 0;
        }

        public bool IsSatisfied<T>(List<GeneticIndividual<T>> pop)
        {            
            _NumIter++;
Console.WriteLine("{0}: {1:F10}", _NumIter, pop[0].Evaluation);
            return (_NumIter >= MaxIter);
        }
    }

    // Stopping conditions for # of no change iter.
    public class NoChangeGeneticStop : IGeneticStopCond
    {
        public readonly int MaxNoChangeIter;
        public int _NumNoChangeIter;

        public double prevBest;

        public NoChangeGeneticStop(int maxNoChangeIter)
        {
            this.prevBest = double.NaN;

            this.MaxNoChangeIter = maxNoChangeIter;
            this._NumNoChangeIter = 0;
        }

        public bool IsSatisfied<T>(List<GeneticIndividual<T>> pop)
        {
            if (pop[0].Evaluation == prevBest)
                _NumNoChangeIter++;
            else
                _NumNoChangeIter = 0;

            return (_NumNoChangeIter >= MaxNoChangeIter);
        }
    }


}
